//
//  UIButton.swift
//  home_ios
//
//  Created by 黄海波 on 16/8/18.
//  Copyright © 2016年 yihua. All rights reserved.
//

import UIKit

extension UIButton {

    func set(image anImage: UIImage?, title: String,
                   titlePosition: UIViewContentMode, additionalSpacing: CGFloat, state: UIControlState){
        self.imageView!.contentMode = .center
        self.setImage(anImage, for: state)
        
        positionLabelRespectToImage(title, position: titlePosition, spacing: additionalSpacing , state:state)
        
        self.titleLabel!.contentMode = .center
        titleLabel?.textAlignment = NSTextAlignment.center
        self.setTitle(title, for: state)
    }
    
    fileprivate func positionLabelRespectToImage(_ title: String, position: UIViewContentMode,
                                             spacing: CGFloat , state: UIControlState) {
        let imageSize = self.imageRect(forContentRect: self.frame)
        let titleFont = self.titleLabel?.font!
        let titleSize = title.size(attributes: [NSFontAttributeName: titleFont!])
        
        var titleInsets: UIEdgeInsets
        var imageInsets: UIEdgeInsets
        
        switch (position){
        case .top:
            titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
        case .bottom:
            if state == UIControlState.selected{
                imageInsets = UIEdgeInsets(top: -10, left: 0, bottom: 0, right: -(titleSize.width))
                titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing),
                                           left: -(imageSize.width+10), bottom: 0, right: 0)
            }else{
                imageInsets = UIEdgeInsets(top: -10, left: 0, bottom: 0, right: -(titleSize.width))
                titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing),
                                           left: -(imageSize.width - 11), bottom: 0, right: 0)
            }
        case .left:
            titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0,
                                       right: -(titleSize.width * 2 + spacing))
        case .right:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        default:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        
        self.titleEdgeInsets = titleInsets
        self.imageEdgeInsets = imageInsets
        
    }
    
//    func setImageForURL(_ url:String? , imageStyle:QiniuUrl_Style , controlState:UIControlState){
//        let newUrl = StringEngine.operateImageUrl(url, imageSizeType: imageStyle)
//        self.sd_setImage(with: URL(string: newUrl), for: controlState)
//    }
//    
//    func setImageForURL(_ url:String? , imageStyle:QiniuUrl_Style ,placeholderImage:UIImage , controlState:UIControlState){
//        let newUrl = StringEngine.operateImageUrl(url, imageSizeType: imageStyle)
//        self.sd_setImage(with: URL(string: newUrl), for: controlState, placeholderImage: placeholderImage)
//    }

}
